package com.vipboot.platform.system.controller;

import cn.hutool.core.util.ObjectUtil;

import com.vipboot.framework.core.domain.LoginUser;
import com.vipboot.framework.core.domain.R;
import com.vipboot.framework.satoken.helper.LoginHelper;
import com.vipboot.platform.system.crud.service.AuthSystemService;
import com.vipboot.platform.system.domain.model.Router;
import com.vipboot.platform.system.domain.model.SysMenuDto;
import com.vipboot.platform.system.domain.model.UserInfoVo;
import com.vipboot.platform.system.domain.vo.SysUserVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * AuthSystem 控制器
 */
@Slf4j
@RequiredArgsConstructor
@RestController
public class AuthSystemController {

    private final AuthSystemService authSystemService;

    /**
     * 获取用户信息
     */
    @GetMapping("/authSystem/getUserInfo")
    public R<UserInfoVo> getUserInfo() {
        UserInfoVo userInfoVo = new UserInfoVo();
        LoginUser loginUser = LoginHelper.getLoginUser();

        SysUserVo user = authSystemService.getUserById(loginUser.getUserId());
        if (ObjectUtil.isNull(user)) {
            return R.fail("没有权限访问用户数据!");
        }
        userInfoVo.setUser(user);
        userInfoVo.setPermissions(loginUser.getMenuPermission());
        userInfoVo.setRoles(loginUser.getRolePermission());
        return R.ok(userInfoVo);
    }

    /**
     * 获取路由信息
     */
    @GetMapping("/authSystem/getRouters")
    public R<List<Router>> getRouters() {
        List<SysMenuDto> menus = authSystemService.listMenuTreeByUserId(LoginHelper.getUserId());
        return R.ok(authSystemService.buildMenuRouters(menus));
    }
}
